<template>
	<view class="page_reg">
		<form class="form-input" @submit="formSubmit" >
			<view>
				<input class="input-phone" name="phoneNumber" type="text" placeholder="手机号" v-model="PHONE"/>
				<input v-if="inviteid" class="input-phone" name="invitephone" type="text" placeholder="推荐人手机号" v-model="invitePHONE"/>
			</view>
			
			<view>
				<button class="submit" type="primary" formType="submit"  >下一步</button>
			</view>
			
			
		</form>
		<text class="tip">我们将向您的手机发送验证短信</text>
	</view>
</template>

<script>
	//来自 graceUI 的表单验证， 使用说明见手册 http://grace.hcoder.net/doc/info/73-3.html
	var  graceChecker = require("../../../common/graceChecker.js");
	export default {
		data() {
			return {
				number: '',
				PHONE: '',
				userId: '',
				invitePHONE:'',
				inviteid:true
			};
		},
		onLoad(options){
			//#ifdef H5
			if(options.userId){
				console.log("该人员有邀请人///////邀请人id是",options.userId);
				this.userId = options.userId;
			}
			//#endif
			if(uni.getStorageSync("inviteid")){
				if(uni.getStorageSync("inviteid")!=undefined &&uni.getStorageSync("inviteid")!='undefined'&&uni.getStorageSync("inviteid")!=''){
						this.inviteid=false
				}
			}
		uni.startPullDownRefresh();
		},
		onPullDownRefresh() {
			console.log('refresh');
			setTimeout(function () {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		watch:{
			number(val) {
				this.PHONE = this.number
			},
			
		},
		
		methods: {
			
			//表单校验
			formSubmit(e) {
				// 将下列代码加入到对应的检查位置
				// 定义表单规则
				var rule = [
					{name:"phoneNumber", checkType : "phoneno",  errorMsg:"请输入正确的手机号"},
					// {name:"invitephone", checkType : "phoneno",  errorMsg:"请输入正确的邀请人手机号"},
				];
				//进行表单检查
				var formData = e.detail.value;
				var checkRes = graceChecker.check(formData, rule);
				if(checkRes){
					console.log(">>>>>>>>>>",e.detail.value)
					this.PHONE = e.detail.value.phoneNumber
					if(this.inviteid==true&&e.detail.value.invitephone){
						//判断邀请人是否存在
						this.$http.post('/user/findUserid',{
							PHONE : e.detail.value.invitephone,
							SIGNATURE : uni.getStorageSync("SIGNATURE"),
						}).then((res)=>{
							//如果存在
							if(res.data.STATE === "0" && res.statusCode === 200){
								//判断是否已注册
								this.$http.post('/user/fingUserByPhone',{
									PHONE : e.detail.value.phoneNumber,
									SIGNATURE : uni.getStorageSync("SIGNATURE"),
								}).then((res1)=>{
									if(res1.data.STATE === "0" && res1.statusCode === 200){
											//页面跳转     已注册人的跳转验证手机
											 uni.navigateTo({
												 url : '/pages/public/wechatLogin/regpwd?PHONE='+ this.PHONE
											})
									}else{
										//页面跳转     未注册的注册跳转
										 uni.navigateTo({
											 url : '/pages/public/wechatLogin/reg-pwd?PHONE='+ this.PHONE +"&userId="+ res.data.OBJECT
										})
									}
								})
								.catch((err1)=>{
								    console.log('request fail', err1);
								})
									
							}else{
								if(res.data.MSG!=''){
									uni.showToast({
										title: res.data.MSG,
										icon: "none",
									})
								}
								
							}
						})
						.catch((err)=>{
						    console.log('request fail', err);
						})
					}else if(this.inviteid==true&&!e.detail.value.invitephone){
						this.$http.post('/common/queryDefaultInvite',{
							SIGNATURE : uni.getStorageSync("SIGNATURE"),
						}).then((res2)=>{
							//判断返回内容
							if(res2.data.STATE === "0" && res2.statusCode === 200){
									//查询邀请人ID
									this.$http.post('/user/findUserid',{
										PHONE : res2.data.OBJECT,
										SIGNATURE : uni.getStorageSync("SIGNATURE"),
									}).then((res3)=>{
										//判断返回内容
										if(res3.data.STATE === "0" && res3.statusCode === 200){
											//判断是否已注册
											this.$http.post('/user/fingUserByPhone',{
												PHONE : e.detail.value.phoneNumber,
												SIGNATURE : uni.getStorageSync("SIGNATURE"),
											}).then((res4)=>{
												if(res4.data.STATE === "0" && res4.statusCode === 200){
														//页面跳转     已注册人的跳转验证手机
														 uni.navigateTo({
															 url : '/pages/public/wechatLogin/regpwd?PHONE='+ this.PHONE 
														})
												}else{
													//页面跳转     未注册的注册跳转
													 uni.navigateTo({
														 url : '/pages/public/wechatLogin/reg-pwd?PHONE='+ this.PHONE +"&userId="+ res3.data.OBJECT
													})
												}
											})
											.catch((err4)=>{
											    console.log('request fail', err4);
											})
										}else{
											if(res3.data.MSG!=''){
												uni.showToast({
													title: res3.data.MSG,
													icon: "none",
												})
											}
										}
									})
									.catch((err3)=>{
									    console.log('request fail', err3);
									})
							}else{
								if(res2.data.MSG!=''){
									uni.showToast({
										title: res2.data.MSG,
										icon: "none",
									})
								}
							}
						})
						.catch((err2)=>{
						    console.log('request fail', err2);
						})
					}else if(this.inviteid==false){
						//判断是否已注册
						this.$http.post('/user/fingUserByPhone',{
							PHONE : e.detail.value.phoneNumber,
							SIGNATURE : uni.getStorageSync("SIGNATURE"),
						}).then((res5)=>{
							if(res5.data.STATE === "0" && res5.statusCode === 200){
									//页面跳转     已注册人的跳转验证手机
									 uni.navigateTo({
										 url : '/pages/public/wechatLogin/regpwd?PHONE='+ this.PHONE
									})
							}else{
								//页面跳转     未注册的注册跳转
								 uni.navigateTo({
									 url : '/pages/public/wechatLogin/reg-pwd?PHONE='+ this.PHONE +"&userId="+ uni.getStorageSync("inviteid")
								})
							}
						})
						.catch((err4)=>{
						    console.log('request fail', err4);
						})
					}
					
					
					//#ifndef H5   
					//页面跳转   
					//  uni.navigateTo({
					// 	 url : '/pages/reg-pwd/reg-pwd?PHONE='+ this.PHONE
					// })
					//#endif
					//#ifdef H5
					// if(this.userId){
					
					// }else{
					// 	// 页面跳转   
					// 	 uni.navigateTo({
					// 		 url : '/pages/reg-pwd/reg-pwd?PHONE='+ this.PHONE
					// 	})
					// }
					//#endif
					
				}else{
					uni.showToast({ title: graceChecker.error, icon: "none" });
				}
			}
		},
	}
</script>

<style>
	page {
		height: auto;
		min-height: 100%;
		background-color: #f5f6f8;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
</style>
<style lang="scss" scoped="">
	$form-border-color: rgba(214, 214, 214, 1);
	$text-color: #B6B6B6;
	
	.page_reg {
		width: 90%;
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.form-input{
		width: 90%;
		
	}
	.input-phone{
		margin-top: 40upx;
		width: 100%;
		border-bottom: 1px solid $form-border-color;
	}
	
	.submit {
		width: 100%;
		margin: 30px 0upx 100upx;
		color: white;
		background-color:#E02E24;
		-webkit-tap-highlight-color: rgba(252, 44, 93, 1.0);
		
		&:active {
			color: #B6B6B6;
			background-color: rgba(252, 44, 93, 0.8);
		}
	}
	
	.tip {
		font-size: 13px;
		color: $text-color;
	}
</style>
